package course.cmsc436.medicationmanager;

import course.cmsc436.medication.Medication;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MedicationListAdapter extends ArrayAdapter<Medication> {

	private final List<Medication> mItems = new ArrayList<Medication>();
	private final Context mContext;
	
	public MedicationListAdapter(Context context) {
		super(context, 0);

		mContext = context;
	}
	
	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Medication getItem(int pos) {
		return mItems.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}
	
	public void setMedication(int pos, Medication med) {
		mItems.set(pos, med);
		notifyDataSetChanged();
	}
	
	public Medication removeMed(int pos){
		Medication m=mItems.remove(pos);
		AlarmScheduler.cancelAlarm(this.getContext(), m);
		notifyDataSetChanged();
		Log.d("REMOVE", "removing"+m.getProprietaryName());
		return m;
	}
	

	@Override
	public void add(Medication item) {
		mItems.add(item);
		notifyDataSetChanged();
	}
	
	public ArrayList<Medication> getMedicationList(){
		return new ArrayList<Medication>(mItems);
	}
	
	public void setMedicationList(ArrayList<Medication> list){
		mItems.clear();
		for (Medication m : list){
			mItems.add(m);
		}
		
		notifyDataSetChanged();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout itemLayout;
		final Medication item = mItems.get(position);
		
		itemLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.med_item, parent, false);
		TextView titleView = (TextView) itemLayout.findViewById(R.id.nameView);
		titleView.setTextColor(Color.BLACK);
		titleView.setText(item.getProprietaryName());
		
		return itemLayout;
	}
}
